/**
 * 
 */
package com.dream.house.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dream.house.entity.UserDTO;
import com.dream.house.service.AllowanceService;
import com.dream.house.service.BuildService;
import com.dream.house.service.BuyService;
import com.dream.house.service.RentService;
import com.dream.house.service.ReturnService;
import com.dream.house.service.RoomService;
import com.dream.house.utils.Constants;

/**
 * @author maoq
 *
 */
@Controller
@RequestMapping("shenpi")
public class ShenpiController {
	
	@Autowired
	BuildService buildService;
	@Autowired
	RoomService roomService;
	@Autowired
	BuyService buyService;
	@Autowired
	RentService rentService;
	@Autowired
	ReturnService returnService;
	@Autowired
	AllowanceService allowanceService;
	
	@ResponseBody
	@RequestMapping("build")
	public String build(String ids, HttpSession session, String status) {
		UserDTO user = (UserDTO)session.getAttribute(Constants.CURRENT_USER);
		
		String[] arrayids = ids.split(",");
		try {
			for (String id : arrayids) {
				buildService.grant(id, status, user.getUserId());
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "0";
		}
		
		return "1";
	}
	
	@ResponseBody
	@RequestMapping("room")
	public String room(String ids, HttpSession session, String status) {
		UserDTO user = (UserDTO)session.getAttribute(Constants.CURRENT_USER);
		
		String[] arrayids = ids.split(",");
		try {
			for (String id : arrayids) {
				roomService.grant(id, status, user.getUserId());
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "0";
		}
		
		return "1";
	}
	
	@ResponseBody
	@RequestMapping("buy")
	public String buy(String ids, HttpSession session, String status) {
		UserDTO user = (UserDTO)session.getAttribute(Constants.CURRENT_USER);
		
		String[] arrayids = ids.split(",");
		try {
			for (String id : arrayids) {
				if (id != null && !id.trim().equals("")) {
					String[] sid = id.split(";");
					buyService.grant(sid[0], sid[1], status, user.getUserId());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "0";
		}
		
		return "1";
	}
	
	@ResponseBody
	@RequestMapping("rent")
	public String rent(String ids, HttpSession session, String status) {
		UserDTO user = (UserDTO)session.getAttribute(Constants.CURRENT_USER);
		
		String[] arrayids = ids.split(",");
		try {
			for (String id : arrayids) {
				if (id != null && !id.trim().equals("")) {
					String[] sid = id.split(";");
					rentService.grant(sid[0], sid[1], status, user.getUserId());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "0";
		}
		
		return "1";
	}
	
	@ResponseBody
	@RequestMapping("returnBuy")
	public String returnBuy(String ids, HttpSession session, String status) {
		UserDTO user = (UserDTO)session.getAttribute(Constants.CURRENT_USER);
		
		String[] arrayids = ids.split(",");
		try {
			for (String id : arrayids) {
				if (id != null && !id.trim().equals("")) {
					String[] sid = id.split(";");
					returnService.grant(sid[0], sid[1], status, user.getUserId());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "0";
		}
		
		return "1";
	}
	
	@ResponseBody
	@RequestMapping("allowance")
	public String allowance(String ids, HttpSession session, String status) {
		UserDTO user = (UserDTO)session.getAttribute(Constants.CURRENT_USER);
		
		String[] arrayids = ids.split(",");
		try {
			for (String id : arrayids) {
//				allowanceService.grant(id, status, user.getUserId());
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "0";
		}
		
		return "1";
	}

}
